package com.lynx.controller;

import com.lynx.controller.model.*;
import com.lynx.service.model.AndroidPluginBO;
import com.lynx.service.model.JobInfoBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.lynx.core.BaseController;
import com.lynx.core.annotation.Login;
import com.lynx.service.AccountService;
import com.lynx.util.EncryptUtil;

/**
 * @author chris.liu
 * @version 4/17/14 11:16 PM
 */
@Controller("accountController")
@RequestMapping(value = "/account", produces = "text/plain;charset=UTF-8")
public class AccountController extends BaseController {

	private static final int MAX_COOKIE_TIMEOUT = 2 * 60 * 60;
	private static final int MAX_SESSION_TIMEOUT = 2 * 60 * 60;
	private static final String COOKIE_DOMAIN = "lynx.net";

	@Autowired
	private AccountService accountService;

	@RequestMapping(value = "/my", method = RequestMethod.GET)
	public ModelAndView profile(@CookieValue(value = "token", required = false) String token) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("my");
		try {
			int uid = EncryptUtil.parseToken(token);
			User user = accountService.getUserInfo(uid);
			if (user != null) {
				user.setToken(token);
				modelAndView.addObject("user", user);
			}
		} catch (Exception e) {

		}
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping(value = "/login", headers = "Accept=application/json", method = RequestMethod.POST)
	public Result login(@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "pwd", required = false) String pwd,
			@CookieValue(value = "token", required = false) String token) {
		Result result = new Result();
		try {
			User user = accountService.login(id, pwd);
			if (user == null) {
				int uid = EncryptUtil.parseToken(token);
				user = accountService.getUserInfo(uid);
				if (user != null) {
					user.setToken(token); // 使用之前的token，防止token有效期无限
				}
			}

			if (user == null) {
                Message msg = new Message("登录失败", "用户不存在或密码不匹配");
                result.setStatusCode(Result.RS_FAIL);
                result.setData(msg);
			} else {
				result.setStatusCode(Result.RS_OK);
				result.setData(user);
			}
		} catch (Exception e) {
            result.setStatusCode(Result.RS_ERROR);
            result.setData(MSG_SYS_ERROR);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public User register(@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "pwd", required = true) String pwd,
			@RequestParam(value = "email", required = true) String email,
			@RequestParam(value = "gender", required = true) Integer gender) {
		try {

		} catch (Exception e) {
            //
		}
		return null;
	}

	@Login
	@ResponseBody
	@RequestMapping(value = "/logout", method = RequestMethod.POST)
	public boolean logout(@RequestParam(value = "id", required = true) String id,
			@RequestParam(value = "pwd", required = true) String pwd) {
		return true;
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public User update(@RequestParam(value = "id", required = true) String id,
			@RequestParam(value = "pwd", required = true) String pwd) {
		try {
			User newInfo = new User();
			User user = accountService.updateUserInfo(newInfo);
			return user;
		} catch (Exception e) {

		}
		return null;
	}
}
